from django.db import models
from django.contrib.auth.models import User


class VirtualScene(models.Model):
    """虚拟疗愈场景表"""
    SCENE_TYPE_CHOICES = [
        ('nature', '自然场景'),
        ('city', '城市场景'),
        ('fantasy', '幻想场景'),
    ]
    
    scene_name = models.CharField(
        max_length=100, 
        verbose_name='场景名称',
        db_comment='虚拟场景名称'
    )
    scene_type = models.CharField(
        max_length=50, 
        choices=SCENE_TYPE_CHOICES, 
        verbose_name='场景类型',
        db_comment='场景类型分类'
    )
    description = models.TextField(
        verbose_name='场景描述',
        db_comment='场景详细描述'
    )
    scene_config = models.JSONField(
        verbose_name='场景配置',
        db_comment='场景3D资源和配置JSON'
    )
    audio_resources = models.JSONField(
        null=True, 
        blank=True, 
        verbose_name='音频资源',
        db_comment='场景配套音频资源JSON'
    )
    recommended_for = models.CharField(
        max_length=200, 
        null=True, 
        blank=True, 
        verbose_name='适用情绪',
        db_comment='推荐的情绪状态'
    )
    is_active = models.BooleanField(
        default=True, 
        verbose_name='是否启用',
        db_comment='场景是否启用'
    )
    created_at = models.DateTimeField(
        auto_now_add=True, 
        verbose_name='创建时间',
        db_comment='记录创建时间'
    )
    updated_at = models.DateTimeField(
        auto_now=True, 
        verbose_name='更新时间',
        db_comment='记录更新时间'
    )

    class Meta:
        db_table = 'xinshi_virtual_scene'
        db_table_comment = '虚拟疗愈场景表'
        verbose_name = '虚拟场景'
        verbose_name_plural = '虚拟场景'

    def __str__(self):
        return self.scene_name


class MeditationCourse(models.Model):
    """冥想课程表"""
    course_name = models.CharField(
        max_length=200, 
        verbose_name='课程名称',
        db_comment='冥想课程名称'
    )
    category = models.CharField(
        max_length=50, 
        verbose_name='课程分类',
        db_comment='课程分类(身体扫描/呼吸冥想/正念冥想等)'
    )
    description = models.TextField(
        verbose_name='课程描述',
        db_comment='课程详细描述'
    )
    duration_minutes = models.IntegerField(
        verbose_name='课程时长',
        db_comment='课程时长(分钟)'
    )
    audio_url = models.URLField(
        verbose_name='音频链接',
        db_comment='引导音频文件URL'
    )
    script = models.TextField(
        null=True, 
        blank=True, 
        verbose_name='引导词',
        db_comment='冥想引导词文本'
    )
    difficulty_level = models.IntegerField(
        default=1, 
        verbose_name='难度等级',
        db_comment='课程难度等级1-5'
    )
    recommended_scene = models.ForeignKey(
        VirtualScene, 
        on_delete=models.SET_NULL, 
        null=True, 
        blank=True,
        verbose_name='推荐场景',
        db_comment='推荐搭配的虚拟场景ID'
    )
    is_active = models.BooleanField(
        default=True, 
        verbose_name='是否启用',
        db_comment='课程是否启用'
    )
    created_at = models.DateTimeField(
        auto_now_add=True, 
        verbose_name='创建时间',
        db_comment='记录创建时间'
    )
    updated_at = models.DateTimeField(
        auto_now=True, 
        verbose_name='更新时间',
        db_comment='记录更新时间'
    )

    class Meta:
        db_table = 'xinshi_meditation_course'
        db_table_comment = '冥想课程表'
        verbose_name = '冥想课程'
        verbose_name_plural = '冥想课程'

    def __str__(self):
        return self.course_name


class MeditationRecord(models.Model):
    """冥想记录表"""
    user = models.ForeignKey(
        User, 
        on_delete=models.CASCADE, 
        verbose_name='用户',
        db_comment='关联用户ID'
    )
    course = models.ForeignKey(
        MeditationCourse, 
        on_delete=models.CASCADE, 
        null=True, 
        blank=True,
        verbose_name='课程',
        db_comment='关联冥想课程ID'
    )
    scene = models.ForeignKey(
        VirtualScene, 
        on_delete=models.SET_NULL, 
        null=True, 
        blank=True,
        verbose_name='场景',
        db_comment='使用的虚拟场景ID'
    )
    duration_minutes = models.IntegerField(
        verbose_name='实际时长',
        db_comment='冥想实际时长(分钟)'
    )
    focus_score = models.IntegerField(
        null=True, 
        blank=True, 
        verbose_name='专注度评分',
        db_comment='冥想专注度自评分1-10'
    )
    mood_before = models.CharField(
        max_length=50, 
        null=True, 
        blank=True, 
        verbose_name='冥想前心情',
        db_comment='冥想前情绪状态'
    )
    mood_after = models.CharField(
        max_length=50, 
        null=True, 
        blank=True, 
        verbose_name='冥想后心情',
        db_comment='冥想后情绪状态'
    )
    notes = models.TextField(
        null=True, 
        blank=True, 
        verbose_name='笔记',
        db_comment='用户的冥想笔记和感受'
    )
    meditation_date = models.DateTimeField(
        verbose_name='冥想日期',
        db_comment='冥想练习日期时间'
    )
    created_at = models.DateTimeField(
        auto_now_add=True, 
        verbose_name='创建时间',
        db_comment='记录创建时间'
    )

    class Meta:
        db_table = 'xinshi_meditation_record'
        db_table_comment = '冥想记录表'
        verbose_name = '冥想记录'
        verbose_name_plural = '冥想记录'
        ordering = ['-meditation_date']

    def __str__(self):
        return f'{self.user.username} - {self.meditation_date}'


class WhiteNoise(models.Model):
    """白噪音资源表"""
    NOISE_TYPE_CHOICES = [
        ('nature', '自然声音'),
        ('urban', '城市声音'),
        ('artificial', '人工白噪音'),
    ]
    
    noise_name = models.CharField(
        max_length=100, 
        verbose_name='名称',
        db_comment='白噪音名称'
    )
    noise_type = models.CharField(
        max_length=50, 
        choices=NOISE_TYPE_CHOICES, 
        verbose_name='类型',
        db_comment='白噪音类型'
    )
    description = models.TextField(
        null=True, 
        blank=True, 
        verbose_name='描述',
        db_comment='音频描述'
    )
    audio_url = models.URLField(
        verbose_name='音频链接',
        db_comment='音频文件URL'
    )
    duration_minutes = models.IntegerField(
        null=True, 
        blank=True, 
        verbose_name='时长',
        db_comment='音频时长(分钟)'
    )
    tags = models.JSONField(
        null=True, 
        blank=True, 
        verbose_name='标签',
        db_comment='音频标签JSON数组'
    )
    is_active = models.BooleanField(
        default=True, 
        verbose_name='是否启用',
        db_comment='资源是否启用'
    )
    created_at = models.DateTimeField(
        auto_now_add=True, 
        verbose_name='创建时间',
        db_comment='记录创建时间'
    )

    class Meta:
        db_table = 'xinshi_white_noise'
        db_table_comment = '白噪音资源表'
        verbose_name = '白噪音'
        verbose_name_plural = '白噪音'

    def __str__(self):
        return self.noise_name
